//获取应用实例
const app = getApp();

Page({
  /**
  * 页面的初始数据
  */
  data: {
    currtab: 0,
    swipertab: [{ name: '淘宝', index: 0 }, { name: '京东', index: 1 }, { name: '拼多多', index: 2}],
    searchRecord: [],
    hotRecord:[]
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.getHotSearch();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow:function(){
    var that=this;
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
    this.getHotSearch();
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        break
    }
  },
  searchSubmit: function (e) {
    console.info('携带数据为：', e.detail.value)
    var that = this
    var inputVal = that.data.recordValue
    var searchRecord = this.data.searchRecord
    if (inputVal == '' || inputVal == undefined) {
      //输入为空时的处理
      console.log("inputVal is null")
    }
    else {
      //将搜索值放入历史记录中,只能放前五条
      if (searchRecord.length < 5) {
        searchRecord.unshift(
          {
            value: inputVal,
            id: searchRecord.length
          }
        )
      }
      else {
        searchRecord.pop()//删掉旧的时间最早的第一条
        searchRecord.unshift(
          {
            value: inputVal,
            id: searchRecord.length
          }
        )
      }
      that.setData({
        searchRecord: searchRecord
      })
      //将历史记录数组整体储存到缓存中
      wx.setStorageSync('searchRecord', searchRecord)
    }
    wx.navigateTo({
      url: '/pages/shop/search-list?name=' + e.detail.value,
    })
  },

  openHistorySearch: function () {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [],//若无储存则为空
    })
  },
  recordInput: function (e) {
    this.setData({
      recordValue: e.detail.value
    })
  },
  search: function () {
    var that = this
    var inputVal = that.data.recordValue
    var searchRecord = this.data.searchRecord
    if (inputVal == '') {
      //输入为空时的处理
    }
    else {
      //将搜索值放入历史记录中,只能放前五条
      if (searchRecord.length < 5) {
        searchRecord.unshift(
          {
            value: inputVal,
            id: searchRecord.length
          }
        )
      }
      else {
        searchRecord.pop()//删掉旧的时间最早的第一条
        searchRecord.unshift(
          {
            value: inputVal,
            id: searchRecord.length
          }
        )
      }
      that.setData({
        searchRecord: searchRecord
      })
      //将历史记录数组整体储存到缓存中
      wx.setStorageSync('searchRecord', searchRecord)
    }
  },



  //清除历史记录
  historyDelFn: function () {
    wx.clearStorageSync('searhRecord')
    this.setData({
      searchRecord: []
    })
  },

//获取热门搜索
  getHotSearch: function () {
    var that=this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Tbk&a=getHotSearch',
      data: {
        num: 10,
        type: that.data.currtab,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        that.setData({
          hotRecord:res.data.data.list
        })
        wx.hideLoading();
      },fail(){
        wx.hideLoading();
      }
    })
    
  },

  hothistoryDelFn:function(){
    this.setData({
      hotRecord:[]
    })
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
});
